package com.zyy.smartcity1129.work;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonObject;
import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.data.JobCompanyInfo;
import com.zyy.smartcity1129.data.JobPostInfo;
import com.zyy.smartcity1129.data.MyResumeQueryInfo;
import com.zyy.smartcity1129.data.ResultInfo;
import com.zyy.smartcity1129.data.UserInfo;
import com.zyy.smartcity1129.util.DateStringUtil;
import com.zyy.smartcity1129.util.Http;

import java.util.Date;

import okhttp3.RequestBody;

/**
 * @ClassName: JobPostFragment$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 6/12/2021$
 * @Version: 1.0
 */
public class JobPostFragment extends BaseFragment {
    private TextView tv1;
    private TextView tv2;
    private TextView tv3;
    private TextView tv4;
    private TextView tv5;
    private TextView tv6;
    private TextView tv7;
    private TextView tv8;
    private Button btn;
    private JobPostInfo.RowsBean jobPost = LookJobFragment.jobPost;
    UserInfo.UserBean userBean;
    int userId = 1112061;
    String userName = "";

    @Override
    protected int layId() {
        return R.layout.f_jobpost;
    }

    @Override
    protected String title() {
        return jobPost.getCompanyName();
    }

    @Override
    protected boolean back() {
        return true;
    }

    @Override
    protected Boolean bottom() {
        return false;
    }

    @Override
    protected void init(View iv) {
        initView(iv);
        tv1.setText("职位名称：" + jobPost.getProfessionName());
        tv2.setText("职位名称：" + jobPost.getName());
        tv3.setText("公司地点：" + jobPost.getAddress());
        tv4.setText("薪资待遇：" + jobPost.getSalary() + "元/月");
        tv5.setText("联系人：" + jobPost.getContacts());
        tv6.setText("职位描述：" + jobPost.getObligation());
        tv7.setText("工作年限：" + jobPost.getNeed());
        th(() -> {
            JobCompanyInfo.DataBean company = Http.getInstance().get("/prod-api/api/job/company/" + jobPost.getCompanyId(), null, JobCompanyInfo.class, 1).getData();
            userBean = Http.getInstance().get("/prod-api/api/common/user/getInfo", null, UserInfo.class, 1).getUser();
            userId = userBean.getUserId();
            userName = userBean.getUserName();
            ui(()->{
                tv8.setText("公司名称：" + company.getCompanyName() + "\n"
                + "公司简介：" + company.getIntroductory());
            });
        });
        btn.setOnClickListener(view -> {
            String date = DateStringUtil.stringToYYMMDD(DateStringUtil.dateToString(new Date()));
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("companyId", jobPost.getCompanyId());
            jsonObject.addProperty("companyName", jobPost.getCompanyName());
//            jsonObject.addProperty("id", jobPost.getId());
            jsonObject.addProperty("money", jobPost.getSalary());
            jsonObject.addProperty("postId", jobPost.getProfessionId());
            jsonObject.addProperty("postName", jobPost.getProfessionName());
            jsonObject.addProperty("satrTime", date);
            jsonObject.addProperty("userId", userId);
            jsonObject.addProperty("userName", userName);
            th(() -> {
                MyResumeQueryInfo.DataBean myResumeQ = Http.getInstance().get("/prod-api/api/job/resume/queryResumeByUserId/" + userId, null, MyResumeQueryInfo.class, 1).getData();
                String experience = myResumeQ.getExperience();
                String education = myResumeQ.getEducation();
                String result = "简历为空，请先补充简历";
                if (experience.equals("") || education.equals("")) {
                    add(new ResumeFragment());
                } else {
                    result = Http.getInstance().get("/prod-api/api/job/deliver", RequestBody.create(Http.m, jsonObject.toString()), ResultInfo.class, 2).getMsg();
                }
                Log.i("TAG", jobPost.getName());
                Log.i("TAG", String.valueOf(jsonObject.size()));
                String finalResult = result;
                ui(() ->{
                    Toast.makeText(getContext(), finalResult, Toast.LENGTH_SHORT).show();
                    if (finalResult.equals("操作成功")) {
                        WorkFragment.bottomMovie.setSelectedItemId(R.id.workDeliveryRecord);
                        remove();
                    }
                });
            });

        });
    }

    private void initView(View iv) {
        tv1 = (TextView) iv.findViewById(R.id.tv1);
        tv2 = (TextView) iv.findViewById(R.id.tv2);
        tv3 = (TextView) iv.findViewById(R.id.tv3);
        tv4 = (TextView) iv.findViewById(R.id.tv4);
        tv5 = (TextView) iv.findViewById(R.id.tv5);
        tv6 = (TextView) iv.findViewById(R.id.tv6);
        tv7 = (TextView) iv.findViewById(R.id.tv7);
        tv8 = (TextView) iv.findViewById(R.id.tv8);
        btn = (Button) iv.findViewById(R.id.btn);
    }
}
